package com.duyutian.controller.hiwodytController;

import com.duyutian.common.CusAccessObjectUtil;
import com.duyutian.common.ServiceResult;
import com.duyutian.model.Article;
import com.duyutian.model.ArticleComment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by dyt on 2018/10/3.
 */
/*评论*/
@Controller
@RequestMapping("/hiwodyt")
public class CommentController {

    //添加评论
    @RequestMapping("/addComment")
    @ResponseBody
    public ServiceResult addConmment(ArticleComment comment, HttpServletRequest request){
        comment.setStatus("1");
        comment.setParentId(0L);
        //获取地址
        comment.setIp(CusAccessObjectUtil.getIpAddress(request));
        comment.setCreated(new Date());
        comment.save();
        //文章设置最后回复时间、评论总数
        Article article = Article.dao.findById(comment.getArticleId());
        article.setCommentCount(article.getCommentCount()+1L);
        article.setCommentTime(new Date());
        article.update();
        return ServiceResult.success();
    }

}
